package cn.butcher21.view;

import cn.butcher21.dao.ChartDao;
import cn.butcher21.utils.Day7;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.DatePicker;
import javafx.scene.input.InputMethodEvent;

import java.util.List;
import java.util.Map;

public class ChartController {

    @FXML
    private BarChart<String, Number> saleChart;

    @FXML
    private CategoryAxis xAxis;

    @FXML
    private NumberAxis yAxis;

    @FXML
    private DatePicker start;

    @FXML
    private DatePicker end;


    @FXML
    void search(ActionEvent event) {
        String startValue = String.valueOf(start.valueProperty().getValue());
        String endValue = String.valueOf(end.valueProperty().getValue());
        String[] split1 = startValue.split("-");
        startValue = "";
        for (String s : split1) {
            startValue += s;
        }

        String[] split2 = endValue.split("-");
        endValue = "";
        for (String s : split2) {
            endValue += s;
        }
        showChart(startValue, endValue);

    }

    @FXML
    void changeStart(InputMethodEvent event) {

    }

    public void showChart(String start, String end) {
        saleChart.getData().clear();

        ObservableList<String> list = FXCollections.observableArrayList();
        ChartDao chartDao = new ChartDao();
        XYChart.Series<String, Number> series1 = new XYChart.Series();
        XYChart.Series<String, Number> series2 = new XYChart.Series();
        XYChart.Series<String, Number> series3 = new XYChart.Series();
        series1.setName("销量");
        series2.setName("成交额");
        series3.setName("利润");
        try {
            List<Map<String, Object>> chartData = chartDao.getChartData(start, end);
            for (Map<String, Object> chartDatum : chartData) {
                list.add((String) chartDatum.get("date"));
                series1.getData().add(new XYChart.Data((String) chartDatum.get("date"), (Integer) chartDatum.get("cost")));
                series2.getData().add(new XYChart.Data((String) chartDatum.get("date"), (Double) chartDatum.get("count")));
                series3.getData().add(new XYChart.Data((String) chartDatum.get("date"), (Double) chartDatum.get("profit")));
            }
            System.out.println(chartData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        xAxis.getCategories().clear();
        xAxis.setCategories(list);
        xAxis.setLabel("日期");
        yAxis.setLabel("数量");

        //Prepare XYChart.Series objects by setting data
        saleChart.getData().addAll(series2);
        saleChart.getData().addAll(series1);
        saleChart.getData().addAll(series3);

    }

    @FXML
    private void initialize() {
        showChart(Day7.getNowBefore7(), Day7.getNow());
    }

}
